package progetto;

import example.hot.InetCoordinates;
import peersim.config.Configuration;
import peersim.core.CommonState;
import peersim.core.Control;
import peersim.core.Network;
import peersim.core.Node;

/**
 * This initialization class collects the simulation parameters from the config
 * file and generates uniformly random 2D-coordinates for each node. The
 * coordinates are distributed on a unit (1.0) square.
 *
 * @author Catia Prandi
 */
public class CoordInitializer implements Control {
	
	// ------------------------------------------------------------------------
    // Parameters
    // ------------------------------------------------------------------------
    /**
     * The protocol to operate on.
     * 
     * @config
     */
    private static final String PAR_PROT = "protocol";

    // ------------------------------------------------------------------------
    // Fields
    // ------------------------------------------------------------------------

    /** Protocol identifier, obtained from config property {@link #PAR_PROT}. */
    private final int coordPid;

    // ------------------------------------------------------------------------
    // Constructor
    // ------------------------------------------------------------------------

    /**
     * Standard constructor that reads the configuration parameters. Invoked by
     * the simulation engine.
     * 
     * @param prefix
     *            the configuration prefix for this class.
     */
    public CoordInitializer(String prefix) {
        coordPid = Configuration.getPid(prefix + "." + PAR_PROT);
    }

    // ------------------------------------------------------------------------
    // Methods
    // ------------------------------------------------------------------------
    /**
     * Initialize the node coordinates. 
     */
	@Override
	public boolean execute() {


		// Set coordinates x,y
		for (int i = 0; i < Network.size(); i++) {
			Node n = Network.get(i);
			DynamicCoordinates prot = (DynamicCoordinates) n.getProtocol(coordPid);
			prot.setX(CommonState.r.nextDouble());
			prot.setY(CommonState.r.nextDouble());
			
		}
	
		
		return false;

	}
	
	
	

}
